import axios,{
    type InternalAxiosRequestConfig,
    type AxiosInstance,
    type AxiosError,
    type AxiosResponse,
    type AxiosRequestConfig,
    type Method,

} from 'axios'


//创建axios实例对象
const server:AxiosInstance =axios.create({
    baseURL:'import.meta.env.VITE_APP_TITLE',
    timeout:1000,
})
//请求拦截器
server.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    return config
},(error:AxiosError)=>{
    return Promise.reject(error)
})
//响应拦截器
server.interceptors.response.use((response:AxiosResponse)=>{
    return response.data
},(error:AxiosError)=>{
    return Promise.reject(error)
})
type DataType<T =any>={
    code:number,
    msg:string,
    data:T;
}
const request=<T =any>(url:string,method:Method="GET",data?:object,options?:AxiosRequestConfig)=>{
    server.request({
        url,
        method,
        [method==="GET" ? "params" : "data"]:data,
        ...options,
    })
}
//get 请求
export const get=<T = any>(url:string,data:object)=>{
    request<T>(url,"GET",data)
};
//post 请求
export const post=<T = any>(url:string,data:object)=>{
    request<T>(url,"POST",data)
}
//put 请求
export const put=<T = any>(url:string,data:object)=>{
    request<T>(url,"PUT",data)
}
//delete 请求
export const del=<T = any>(url:string,data:object)=>{
    request<T>(url,"DELETE",data)
}
export default request